//
//  Helper.swift
//  swift3.0Exercise
//
//  Created by Aaron Liu on 2017/1/6.
//  Copyright © 2017年 BMW. All rights reserved.
//

import UIKit

class Helper: NSObject {
    
    class func writeJson() {
        let path = Bundle.main.path(forResource: "city", ofType: "json")
        let data = try! Data(contentsOf: URL(fileURLWithPath: path!), options: .alwaysMapped)
        let dictionary = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
        
        //        let array = dictionary as! NSArray
        let array = dictionary as! [[String: AnyObject]]
        //
        var myArray = [] as [[String: AnyObject]]
        
        for item in array {
            var myDic = [:] as [String: AnyObject]
            for (key,val) in item {
                myDic[key] = val
                if key == "name" {
                    myDic["pinin"] = (val as! String).pinyinCharactor as AnyObject?
                    myDic["first"] = (val as! String).pinyinFirstCharactor as AnyObject?
                }
            }
            myArray.append(myDic)
        }
        
        let sortedArray = myArray.sorted {
            $0["first"]?.compare($1["first"]! as! String, options: .numeric) == .orderedAscending
        }
        
        let adata = try! JSONSerialization.data(withJSONObject: sortedArray, options: .prettyPrinted)
        let url = "/Users/lpz191/Desktop/未命名文件夹/newJson.json"
        let urls = URL(fileURLWithPath: url)
        try! adata.write(to: urls)
    }
    
    class func writeCityJson() {
        let path = Bundle.main.path(forResource: "cities", ofType: "txt")
        let data = try! Data(contentsOf: URL(fileURLWithPath: path!), options: .alwaysMapped)
        let string = String(data: data, encoding: .utf8)
        let array = string?.components(separatedBy: "\n")
        var cityArray = [] as [[String: String]]
        array?.forEach({ (element) in
            var dic = [ : ] as [String : String]
            dic["name"] = element
            dic["province"] = ""
            dic["pinyin"] = element.pinyinCharactor
            dic["firstLetter"] = element.pinyinFirstCharactor
            dic["newPinyin"] = element.pinyinCharactor.replacingOccurrences(of: " ", with: "")
            let short = element.pinyinCharactor.components(separatedBy: " ")
            var letters = ""
            short.forEach({ (str) in
                letters.append((str as NSString).substring(to: 1))
            })
            //            dic["shortPinyin"] = letters
            cityArray.append(dic)
        })
        
        let sortedArray = cityArray.sorted {
            $0["pinyin"]?.compare($1["pinyin"]!, options: .numeric) == .orderedAscending
        }
        
        
        let adata = try! JSONSerialization.data(withJSONObject: sortedArray, options: .prettyPrinted)
        let url = "/Users/lpz191/Desktop/未命名文件夹/newJson.json"
        let urls = URL(fileURLWithPath: url)
        try! adata.write(to: urls)
        
    }

}
